import { messages } from "share/common";
import React from 'react'
import { connect } from 'react-redux'
import { Form, Radio, Button, message, Row, Col } from 'antd'
const RadioGroup = Radio.Group;
const FormItem = Form.Item;
import creditModelService from 'containers/credit-setting/credit-model/credit-model.service'
import errorMessage from 'share/errorMessage'

class NewCreditConfig extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      data: {},
      newCreditConfig: {}
    };
  }

  onCancel = () => {
    this.props.close();
  };

  componentWillReceiveProps(nextProps) {
    this.setState({
      newCreditConfig: nextProps.params.newCreditConfig
    })
  }

  handleSave = (e) => {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        let { newCreditConfig } = this.state;
        let params = Object.assign({},  newCreditConfig, values);
        this.setState({ loading: true });
        creditModelService.putCreditConfig( params).then(res => {
          this.setState({ loading: false });
          message.success(messages('finance-3.key80')/*保存成功*/);
          this.props.close(true);
        }).catch(e => {
          errorMessage(e.response);
          this.setState({ loading: false });
        })
      }
    });
  };


  render() {
    const { getFieldDecorator } = this.props.form;
    const { loading, newCreditConfig } = this.state;
    const formItemLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 12, offset: 1 },
    };
    return (
      <div>
        <Form onSubmit={this.handleSave} className="new-form">
          <FormItem {...formItemLayout} label={messages('finance-3.key153')/*信用分员工可见性*/}>
            {getFieldDecorator('show', {
              initialValue: newCreditConfig.show
            })(
              <RadioGroup>
                <Col span={5}>
                  <Radio value={true}>
                    <span>{messages('finance-3.key150')/*可见*/}</span>
                  </Radio>
                </Col>
                <Col span={5} offset={1}>
                  <Radio value={false}>
                    <span>{messages('finance-3.key149')/*不可见*/}</span>
                  </Radio>
                </Col>
              </RadioGroup>
            )}
          </FormItem>

          <FormItem {...formItemLayout} extra={messages('finance-3.key169')/*可在此配置租户级的信用相关基础配置*/} label={messages('finance-3.key170')/*信用事件写入审批历史*/}>
            {getFieldDecorator('writeApprovalHistoryEnabled', {
              initialValue: newCreditConfig.writeApprovalHistoryEnabled
            })(
              <RadioGroup>
                <Col span={5}>
                  <Radio value={true}>
                    <span>{messages('common.yes')/*是*/}</span>
                  </Radio>
                </Col>
                <Col span={5} offset={1}>
                  <Radio value={false}>
                    <span>{messages('common.no')/*否*/}</span>
                  </Radio>
                </Col>
              </RadioGroup>
            )}
          </FormItem>

          <div className="slide-footer">
            <Button type="primary" htmlType="submit" loading={loading}>{messages('common.save')/*保存*/}</Button>
            <Button onClick={this.onCancel}>{messages('common.cancel')/*取消*/}</Button>
          </div>
        </Form>
      </div>
    )
  }

}

function mapStateToProps(state) {
  return {
    company: state.login.company,
    creditModel: state.creditSetting.creditModel
  }
}
const WrappedNewCreditConfig = Form.create()(NewCreditConfig);

export default connect(mapStateToProps)(WrappedNewCreditConfig);
